/***********************************************************************
** Copyright (C) 2011 Anthony Naddeo <anthony.naddeo@gmail.com>
**
** This file is part of LinkedUp
**
** LinkedUp is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**
** LinkedUp is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this package; If not, see <http://www.gnu.org/licenses/>.
**
***********************************************************************/
import QtQuick 1.1
import com.meego 1.0
import "../components"

MyPage {
	id: root
	anchors.fill: parent
	title: "Company"

	tools: ToolBarLayout {
		ToolIcon { platformIconId: "toolbar-view-menu";
			anchors.right: parent===undefined ? undefined : parent.right
			onClicked: (myMenu.status == DialogStatus.Closed) ? myMenu.open() : myMenu.close()
		}
		ToolIcon { platformIconId: "toolbar-back";
			anchors.left: parent===undefined ? undefined : parent.left
			visible: pageStack ? pageStack.depth > 1 ? true : false : false
			onClicked: {
				if(pageStack.depth > 1)
					pageStack.pop()
			}
		}
		ButtonRow{
			anchors.horizontalCenter: parent.horizontalCenter
			width: parent.width
			spacing: 30
			ToolIcon {
				id: star
				property bool marked: User.following(profileID)
				platformIconId: marked ? "toolbar-favorite-mark" : "toolbar-favorite-unmark"
				/*uncomment the following when they implement DELETE method*/
	//			onClicked: marked ? httpDelete(API.delete_company_following(profileID)) : post(API.post_company_follow(profileID))
				onClicked: marked ? Qt.openUrlExternally("http://www.linkedin.com/company/"+model.get(0).universalName) : post(API.post_company_follow(profileID))
			}
			ToolIcon{
				platformIconId: "toolbar-list"
				onClicked: {
					var comp = Qt.createComponent("../news/News.qml")
					var page = comp.createObject(root, {"searchString":model.get(0).name, "title":model.get(0).name+" News"})
					pageStack.push(page)
				}
			}
			ToolIcon{id: stockButton
				platformIconId: "toolbar-gallery"
				onClicked: {
					var comp = Qt.createComponent("../finance/Finance.qml")
					var page = comp.createObject(root, {"ticker":model.get(0).ticker, "title":"NASDAQ: "+model.get(0).ticker})
					pageStack.push(page)
				}
			}
		}




	}

	Component.onCompleted: {
		get(API.get_company_by_id(profileID))
	}

	onGetFinished: model.xml = responseText

	onPostFinished: {
		if(statusCode === "201"){
			star.marked = true
			User.follow(profileID,model.get(0).name) //add the company id to the QHash
		}
	}
	onDeleteFinished:{
		if(statusCode === "201"){
			star.marked = false
			User.stopFollow(profileID) //remove the company id from the QHash
		}
	}


	property string profileID


	CompanyModel{
		id: model
		onStatusChanged: {
			if(status == XmlListModel.Ready && get(0)){
				root.title = get(0).name
				stockButton.enabled = get(0).ticker
			}

		}
	}

	ListView{
		id: general
		anchors.fill: parent
		anchors.topMargin: borderHeight+10
		anchors.leftMargin: 10
		anchors.rightMargin: 10

		model: model
		delegate: CompanyDelegate{}
	}



}
